﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VoxelBlock.VoxelBlock3D;
using VoxelBlock;
using Vector3Int = VoxelBlock.Vector3Int;


public class OperationImplementBrushSelect : OperationImplement
{
    VoxelCategories.OperationSelectOperator selectOperator;
    List<Vector3Int> oldSerializeVoxel = new List<Vector3Int>();
    Vector3Int[] zeroVector3Int = new Vector3Int[0];

    //public OperationImplementBrushSelect(ProxyOperation proxy) : base(proxy)
    //{

    //}

    public override void Start(StartArg arg)
    {
        base.Start(arg);
        this.selectOperator = arg.selectOperator;
        oldSerializeVoxel = new List<Vector3Int>();
    }

    public override void Run(RunArg arg)
    {
        var array = BasicAlgorithm.GetLineBlock3D(arg.updateStartPosition, arg.nowSelectPosition);

        switch (selectOperator)
        {
            case VoxelCategories.OperationSelectOperator.Union:
                oldSerializeVoxel.AddRange(nowDrawVoxel3DCore.DrawSelect(array, 1));
                break;
            case VoxelCategories.OperationSelectOperator.Remove:
                oldSerializeVoxel.AddRange(nowDrawVoxel3DCore.DrawSelect(array, 255));
                break;
            default:
                break;
        }
    }

    public override void Over(OverArg arg)
    {
        if (oldSerializeVoxel.Count > 0)
        {
            switch (selectOperator)
            {
                case VoxelCategories.OperationSelectOperator.Union:
                    proxy.Write(VoxelCategories.Operation.SelectAdd, zeroVector3Int, oldSerializeVoxel);
                    oldSerializeVoxel = new List<Vector3Int>();
                    break;
                case VoxelCategories.OperationSelectOperator.Remove:
                    proxy.Write(VoxelCategories.Operation.SelectRemove, oldSerializeVoxel, zeroVector3Int);
                    oldSerializeVoxel = new List<Vector3Int>();
                    break;
                default:
                    break;
            }
        }
        else
        {
            List<Vector3Int> serializeVoxel = new List<Vector3Int> { arg.nowSelectPosition };
            switch (selectOperator)
            {
                case VoxelCategories.OperationSelectOperator.Union:
                    nowDrawVoxel3DCore.DrawSelect(serializeVoxel, 1);
                    proxy.Write(VoxelCategories.Operation.SelectAdd, zeroVector3Int, serializeVoxel);
                    break;
                case VoxelCategories.OperationSelectOperator.Remove:
                    nowDrawVoxel3DCore.DrawSelect(serializeVoxel, 255);
                    proxy.Write(VoxelCategories.Operation.SelectRemove, serializeVoxel, zeroVector3Int);
                    break;
                default:
                    break;
            }
        }
    }
}
